package cq.zhcqk.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dou361.dialogui.DialogUIUtils;
import com.dou361.dialogui.bean.BuildBean;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import cq.zhcqk.R;
import cq.zhcqk.util.Common;
import cq.zhcqk.util.ImageLoaderHelper;
import cq.zhcqk.widget.ZoomableImageView;

public class ShowWebImageActivity extends Activity {
    /**
     *  在一个activity中销毁指定activity
     */

    public  static ShowWebImageActivity showWebImageActivity;
    private TextView imageTextView = null;
    private String imagePath = null;
    private ZoomableImageView imageView = null;
    protected BuildBean buildBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showWebImageActivity=this;
        setContentView(R.layout.show_webimage);
        this.imagePath = getIntent().getStringExtra("image");

        buildBean = DialogUIUtils.showLoading(this, "加载中...", true, true, true, false);

      /*this.imageTextView = (TextView) findViewById(R.id.show_webimage_imagepath_textview);
      imageTextView.setText(this.imagePath);*/
        imageView = (ZoomableImageView) findViewById(R.id.show_webimage_imageview);
//        ImageLoader.getInstance().displayImage(imagePath,imageView, ImageLoaderHelper.getOptionsDfBig());
        ImageLoader.getInstance().displayImage(imagePath, imageView, ImageLoaderHelper.getOptionsDfBig(), new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {
                //开始加载的时候执行
                buildBean.show();
            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                //加载失败的时候执行
                DialogUIUtils.dismiss(buildBean);
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                //加载成功的时候执行
                DialogUIUtils.dismiss(buildBean);
            }

            @Override
            public void onLoadingCancelled(String imageUri, View view) {
                //加载取消的时候执行
                DialogUIUtils.dismiss(buildBean);
            }

        });
//        try {
//            imageView.setImageBitmap(((BitmapDrawable) ShowWebImageActivity.loadImageFromUrl(this.imagePath)).getBitmap());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }

    public static Drawable loadImageFromUrl(String url) throws IOException {

        URL m = new URL(url);
        InputStream i = (InputStream) m.getContent();
        Drawable d = Drawable.createFromStream(i, "src");
        return d;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i("zlb","zlb===");
    }
}
